package com.autotest.sync.android;

import com.android.ddmlib.IDevice;
import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
import com.autotest.sync.utils.ADBHelper;
import com.autotest.sync.utils.ThreadManager;
import com.autotest.sync.utils.TimeUtil;
import com.google.common.base.Objects;

/**
 * 同步方案，唯一对外接口
 * @author zy
 *
 */
public class Sync{
	private static Sync instance = null;
	private MasterDevice mMasterDevice;
	private int current_status = 0; // 0 未开始，1开始 
	
	private DeviceChangeListener listenser = null;
	
	public interface DeviceChangeListener{
		public void add(String serial);
		public void remove(String serial);
	}
	
	private IDeviceChangeListener iDeviceChangeListener = new IDeviceChangeListener() {

		@Override
		public void deviceDisconnected(IDevice device) {
			System.out.println(device.getSerialNumber() + ": 断开连接");
			if(mMasterDevice != null){
				if(Objects.equal(device.getSerialNumber(), mMasterDevice.serial)){
					if(current_status == 1){
						mMasterDevice.stop();
						return;
					}
				}
			}
			if(listenser != null){
				listenser.remove(device.getSerialNumber());
			}
			if(mMasterDevice!= null){
				MyEventBus.getInstance().unRegister(device.getSerialNumber());
				System.out.println(String.format("%s slave 卸载完成", device.getSerialNumber()));
			}
			
		}

		@Override
		public void deviceConnected(final IDevice device) {
			System.out.println(device.getSerialNumber() + ": 已连接");
			if(mMasterDevice != null){
				if(Objects.equal(device.getSerialNumber(), mMasterDevice.serial)){
					if(current_status == 1){
						ThreadManager.getInstance().start(()-> mMasterDevice.restart());
						return;
					}
				}else{
					registerDevice(device.getSerialNumber());
				}
			}
			
			if(listenser != null){
				listenser.add(device.getSerialNumber());
			}
		}

		@Override
		public void deviceChanged(IDevice device, int arg1) {
			
		}
	};
	
	private Sync(){
		ADBHelper.registerDeviceListener(iDeviceChangeListener);
	}
	
	public static Sync getInstance(){
		if(instance == null){
			synchronized (Sync.class) {
				instance = new Sync();
			}
		}
		return instance;
	}
	
	/**
	 * 监听设备连接状态
	 * @param listenser
	 */
	public void addListener(DeviceChangeListener listenser){
		this.listenser = listenser;
	}
	
	
	public void setMaster(String serial,String inputEvent){
		mMasterDevice = new MasterDevice(serial,inputEvent);
	}
	
	public void start(){
		current_status = 1;
		ThreadManager.getInstance().start(()->{ 
			String[] serials = getOnlineDevices();
			for(String serial : serials){
				if(serial == mMasterDevice.serial){
					continue;
				}
				registerDevice(serial);
			}
			mMasterDevice.start();
		});
	}
	
	/**
	 * 切换主设备发送数据状态
	 */
	public void switchPause(){
		mMasterDevice.setPause();
	}
	
	public void stop(){
		current_status = 0;
		ThreadManager.getInstance().start(()->{
			if(mMasterDevice != null){
				mMasterDevice.stop();
			}
			mMasterDevice = null;
			MyEventBus.getInstance().clear();
		});
	}
	
	public void exit(){
		ADBHelper.removeDeviceListener(iDeviceChangeListener);
		stop();
	}
	
	public int getStatus(){
		return current_status;
	}
	/**
	 * 注册设备到事件总线
	 * @param serial
	 */
	public void registerDevice(String serial){
		final SlaveDevice synDevice = new SlaveDevice(serial);
		MyEventBus.getInstance().register(synDevice);
		ThreadManager.getInstance().start(new Runnable() {
			
			@Override
			public void run() {
				synDevice.startMiniTouch();
				System.out.println(String.format("%s slave 加载完成", serial));
			}
		});
	}
	
	public void addSlaves(){
		String[] serials = ADBHelper.getOnlineDevices();
		if(serials != null){
			for(String serial : serials){
				if(serial != this.mMasterDevice.serial){
					registerDevice(serial);
				}
			}
		}
	}
	
	public String[] getOnlineDevices(){
		return ADBHelper.getDevicesName();
	}
	
	public static void main(String[] args) {
		Sync.getInstance().setMaster("TO8DKFIZ99999999",null);
		Sync.getInstance().start();
	}

}
